package org.brisling.sysman.queryService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.util.WhereUtil;
import org.brisling.sysman.domain.UserRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/userRole/query")
public class QueryUserRoleService extends AbstractBaseQueryService<UserRoles>{
	
	private HashMap<String,String> typeMap =null;
	
	private HashMap<String,String> orderMap =null;
	public QueryUserRoleService(){
		this.moduleName = ConstSysParam.MODULE_USER_ROLE;
		this.subSystem = ConstSysParam.SUBSYSTEM_SYSMAN;
		this.tableName = ConstSysParam.VIEW_PREFIX+this.getTableName(UserRoles.class);
		this.clazz = UserRoles.class;
		typeMap = this.getTypeMap(clazz);
		
		orderMap = new HashMap<String,String>();
		orderMap.put("sortedid","");	
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}
	
	
	@SuppressWarnings({ "rawtypes" })
	@RequestMapping("/listDomain.action")
	public ListDomain<LinkedHashMap> getListDomain(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="sort",required=false) String sort){		
		
		
		return super.getPagedListDomain(page, rows);
	}
	
	public List<UserRoles> getListAll(Map<String, String> map){	
		
		String sql = "";
		String whereCorse = null;					
			
		whereCorse = WhereUtil.getConditionWhereCorse(map,typeMap);
		System.out.println("whereCorse: " +whereCorse);		
		
		if((whereCorse!=null)&&(whereCorse.trim().length()>0)){			
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
			return super.getAllList(sql);
		}
		return new ArrayList<UserRoles>();		
		
	}
		
	@RequestMapping("/getOne.action")
	public UserRoles getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}

}
